package test01.controller.miaosha;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import test01.Dto.RequestDto.LoginRequestDto;
import test01.Dto.RequestDto.LoginTwoRequestDto;
import test01.Exception.GlobalException;
import test01.Result.CodeMsg;
import test01.Result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import static test01.Result.CodeMsg.GLOBAL_SERVICE_ERROR;

@Slf4j
@Controller
@RequestMapping("ms")
public class UserController {

    @GetMapping("/login")
    public String index(HttpServletRequest httpServletRequest){
        System.out.println("执行login逻辑");
        HttpSession session = httpServletRequest.getSession();
        session.setAttribute("login","1");
        return "index.html";
    }
    @GetMapping("/success")
    @ResponseBody
    public Result success(){
        log.info("执行login逻辑");
        return Result.success("success");
    }
    @GetMapping("/error")
    @ResponseBody
    public Result error(){
        log.info("执行login逻辑");
        return Result.error(new CodeMsg(1,"error"));
    }

    @PostMapping ("/check")
    @ResponseBody
    public Result success(@Valid LoginRequestDto loginRequestDto){
        log.info("执行login逻辑");
        if (loginRequestDto.getAge()<=0){
            throw new GlobalException(GLOBAL_SERVICE_ERROR);
        }
        return Result.success("success");
    }
    @PostMapping ("/getToken")
    @ResponseBody
    public Result getToken(){
        log.info("执行login逻辑");
        return Result.success("success");
    }
}
